package net.assassin8.bautista.stalker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initMain();
	}

	public void initMain() {
		setContentView(R.layout.main);

		Button startButton = (Button) findViewById(R.id.StartButton);

		startButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Launch SelectMallActivity
				Intent myIntent = new Intent(MainActivity.this,
						SelectMallActivity.class);
				MainActivity.this.startActivity(myIntent);
			}

		});

		Button uploadButton = (Button) findViewById(R.id.UploadButton);

		uploadButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Launch SelectMallActivity
				Intent myIntent = new Intent(MainActivity.this,
						UploadActivity.class);
				MainActivity.this.startActivity(myIntent);
			}

		});

		Button exitButton = (Button) findViewById(R.id.ExitButton);

		exitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				MainActivity.this.finish();
			}
		});
	}

	@SuppressWarnings("unused")
	private void copyAssets() {
		AssetManager assetManager = getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}
		for (int i = 0; i < files.length; i++) {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = assetManager.open(files[i]);
				out = new FileOutputStream(
						"/data/data/net.assassin8.bautista.stalker/" + files[i]);
				copyFile(in, out);
				in.close();
				in = null;
				out.flush();
				out.close();
				out = null;
			} catch (Exception e) {
				Log.e(TAG, e.getMessage());
			}
		}
	}

	private void copyFile(InputStream in, OutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
	}
}